import ad from "./Ad";

export default {
    isHome: true, //是否在首页
    aldMess: function (keyname, val) {
        keyname = keyname + "";
        return;
        console.log("阿拉丁事件:", keyname, val);
        //阿拉丁事件
        if (this.iswx) {
            if (val == undefined) {
                wx.aldSendEvent(keyname);
            } else {
                wx.aldSendEvent(keyname, val);
            }
        }
    },
    ad: ad,
    iswx: false,
    playChaping: function (type) {
        //播放插屏
        ad.chapingObj.show(type);
    },
    playAd: function (succ, err) {
        //播放视频
        ad.videoObj.playOne("adunit-7ca426fe12192870", succ, err);
    },
    getShare: function (types, obj) {
        var num = 1 + parseInt(Math.random() * 2);
        var imageUrl = "shareimg/share" + num + ".png";
        var title = "@所有人，快来一起喵喵喵！";
        var query = "";
        var shareObj = {
            imageUrl: imageUrl,
            title: title,
            query: query
        };
        console.log("shareObj:", shareObj);
        return shareObj;
    },
    share: function (type, obj, succ, err) {
        //分享
        var shareObj = this.getShare(type, obj);
        this.isshare = true;
        this.sharetime = Date.now();
        this.sharesucc = succ;
        this.shareerr = err;
        if (this.iswx) {
            wx.shareAppMessage(shareObj);
        } else {
            if (this.sharesucc) {
                this.sharesucc();
            }
        }
    },
    cachedata: {},
    dateKey: "gongjian2",
    saveData: function () {
        // 更新数据
        // console.log("save");
        Laya.LocalStorage.setJSON(this.dateKey, this.cachedata);
    },
    getTodayKeyTime: function (ns) {
        //得到今天年月日
        var date = new Date(parseInt(ns) * 1000);
        var nian = date.getFullYear(); // 获取完整的年份(4位,1970)
        var yue = date.getMonth() + 1; // 获取月份(0-11,0代表1月,用的时候记得加上1)
        var ri = date.getDate(); // 获取日(1-31)
        var key = nian + "-" + yue + "-" + ri;
        // console.log(key);
        return key;
    },
    init: function () {
        var parent = this;
        var userdata = Laya.LocalStorage.getJSON(this.dateKey);
        if (!userdata) {
            userdata = {
                id: "",
                muc: 1,
                yinxiao: 1,
                coin: 0, //金币
            }
            Laya.LocalStorage.setJSON(this.dateKey, userdata);
        }
        //上一把过关分数，永久累计,过关更新
        this.cachedata = userdata;

        this.sys = {
            width: Laya.Browser.clientWidth,
            height: Laya.Browser.clientHeight
        }
        this.isiphoneX = this.sys.height / this.sys.width >= 2;
        ad.setUtil(this);

        if (Laya.Browser && Laya.Browser.window && Laya.Browser.window.wx != undefined) {
            this.wx = wx;
            this.iswx = true;
            wx.showShareMenu({
                withShareTicket: true,
                menus: ['shareAppMessage', 'shareTimeline']
            });
            wx.onShareAppMessage(function () {
                // 用户点击了“转发”按钮
                return parent.getShare();
            });
            var launchobj = wx.getLaunchOptionsSync();
            this.searchObj = launchobj.query;

            if (typeof wx.getUpdateManager === 'function') {
                var updateManager = wx.getUpdateManager();
                updateManager.onUpdateReady(function () {
                    // 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
                    updateManager.applyUpdate();
                });
            }

            console.log("this.searchObj:", this.searchObj);

            var isqidong = false;
            var isf5bann = false;

            var stopMucT;
            wx.onShow(function (e) {
                var query = e.query;
                console.log("onshow:", e, query);
                // parent.onEnterShu(e);
                if (isf5bann) {
                    isf5bann = false;
                    ad.guanggaobannObj.openBanner();
                    console.log("打开新的");
                }
                ad.guanggaobannObj.bannerAdResize();

                if (parent.isshare) {
                    parent.isshare = false;
                    if (Date.now() - parent.sharetime >= 2000) {
                        if (Math.random() >= 0.1) {
                            if (parent.sharesucc) {
                                parent.sharesucc();
                            }
                        } else {
                            if (parent.shareerr) {
                                parent.shareerr();
                            }
                        }
                    } else {
                        if (parent.shareerr) {
                            parent.shareerr();
                        }
                    }
                }

                if (e && e.query && e.query.channel != undefined) {
                    parent.aldMess("来源:", {
                        "channel": (e.query.channel + "")
                    });
                }
                if (!isqidong) {
                    isqidong = true;
                } else {

                }
                //小程序启动时，不管是热启动还是冷启动
                parent.aldMess("热启动");
            });
            wx.onHide(function (e) {
                isf5bann = false;
                clearTimeout(parent.playMT);
                // Laya.SoundManager.stopAll();
                if (ad.guanggaobannObj.bannertype == "show") {
                    if (e && (e.targetAction >= 7)) {
                        isf5bann = true;
                        ad.guanggaobannObj.hideBanner(true);
                    }
                }
            });
            if (wx.onWindowResize) {
                //尺寸改变
                wx.onWindowResize(function () {

                });
            }

            var channel = "1000";
            if (this.searchObj.channel != undefined) {
                channel = this.searchObj.channel;
            }
            // this.wxlogin(channel);
        }
        ad.createCustomObj.init(Laya.Browser.clientWidth, this.isiphoneX);

        console.log("-----是不是微信：", this.iswx ? "是" : "不是");
    },
    setOpenSize: function (isgame) {
        // if (this.wxopenData) {
        //     this.wxopenData.canvas.height = isgame ? 170 : 940;
        // }
    },
    postMessage: function (obj) {
        if (this.wxopenData) {
            this.wxopenData.postMessage(obj);
        }
        // console.log("发送Mess:", obj);
    },
    playMusic: function () {
        return;
        // Laya.SoundManager.stopMusic();
        if (this.cachedata.muc != 1) {
            //播放音乐
            Laya.SoundManager.playMusic(this.baseurl + "muc/bg.mp3", 1);
        }
    },
    soundObj: {},
    playSound: function (name) {
        //播放音效
        return;
        var baserurl1 = "res/muc/";
        Laya.SoundManager.playSound(baserurl1 + name + ".mp3", 1);
    },
    getTodayKey: function (times) {
        //得到今天年月日
        var date = new Date();
        if (times != undefined) {
            date = new Date(parseInt(times) * 1000);
        }
        var nian = date.getFullYear(); // 获取完整的年份(4位,1970)
        var yue = date.getMonth() + 1; // 获取月份(0-11,0代表1月,用的时候记得加上1)
        var ri = date.getDate(); // 获取日(1-31)
        var key = nian + "-" + yue + "-" + ri;
        // console.log(key);
        return key;
    },
    addCoin: function (coin, issave) {
        //添加金币
        this.cachedata.coin += coin;
        // if (issave) {
        this.saveData();
        // }
    },
    loadimg_loading: function (call, jinducall) {
        var parent = this;
        var Handler = Laya.Handler;
        var imgarr = [
            "res/bg/loadingbg.jpg",
            "res/loading/Logo0.png",
            "res/atlas/res/loading.atlas"
        ]
        Laya.loader.load(imgarr, Handler.create(this, function () {
            call();
        }), Handler.create(this, function (e) {
            //加载进度
            // console.log(e);
            if (jinducall) {
                jinducall(e);
            }
        }, [], false));
    },
    loadimg_home: function (call, jinducall) {
        var parent = this;
        var Handler = Laya.Handler;
        var Templet = Laya.Templet;
        var Event = Laya.Event;
        var imgarr = [
            "res/home/bg.png",
            "res/game/bg1_1.jpg",
            "res/game/bg1_2.jpg",
            "res/game/border1_1.png",
            "res/game/border1_2.png",
            "res/home/Bg_Floor.png",
            "res/home/Bg_Gamemodel.png",
            "res/atlas/res.atlas",
            "res/atlas/res/chengjiu.atlas",
            "res/atlas/res/chengjiu/item.atlas",
            "res/atlas/res/meiri.atlas",
            "res/atlas/res/game.atlas",
            "res/atlas/res/home.atlas",
            "res/atlas/res/shezhi.atlas",
            "res/atlas/res/home/zuanshi.atlas",
            "res/atlas/res/home/moshi.atlas"
        ]
        Laya.loader.load(imgarr, Handler.create(this, function () {
            parent.loadskArr([
                ["role1", "sk/RobinHood2x0.sk"]
                // ["pipei", "sk/匹配动效.sk"],
                // ["guochang", "sk/过场效果.sk"]
            ], function () {
                call();
            }, function (e) {
                if (jinducall) {
                    jinducall(0.9 + e * 0.1);
                }
            });
        }), Handler.create(this, function (e) {
            //加载进度
            // console.log(e);
            if (jinducall) {
                jinducall(e);
            }
        }, [], false));
    },
    AnimationPlay: function (obj, loop, time) {
        //time= 间隔+ 当前
        if (loop) {
            obj.play(0, true);
        } else {
            obj.play(0, false);
            clearInterval(obj._plT);
            obj._plT = setInterval(function () {
                obj.play(0, false);
            }, time);
        }
    },

    loadskArr: function (arr, call, loadone) {
        var len = arr.length;
        if (len == 0) {
            call();
            return;
        }
        var loadnum = 0;
        for (var i = 0; i < len; i++) {
            this.loadsk(arr[i][0], arr[i][1], function () {
                loadnum++;
                if (loadone) {
                    loadone(loadnum / len);
                }
                if (loadnum >= len) {
                    call();
                }
            });
        }
    },
    loadsk: function (key, src, call) {
        var Handler = Laya.Handler;
        var Templet = Laya.Templet;
        var Event = Laya.Event;
        this.skObj[key] = new Templet();
        // this.skObj[key] = new Laya.SpineTemplet();//3.8
        this.skObj[key].on(Event.COMPLETE, this, function () {
            call();
        });
        this.skObj[key].on(Event.ERROR, this, function (e) {
            console.log("加载错误", e);
        });
        this.skObj[key].loadAni(src);
    },
    skObj: {},
    skArr: [],
    getSk: function (name, call, iscache) {
        if (!this.skObj[name]) {
            console.log("not getSk:name:", name);
        }
        //创建动画 0,使用模板缓冲的数据，模板缓冲的数据，不允许修改 （内存开销小，计算开销小，不支持换装） 
        //1,使用动画自己的缓冲区，每个动画都会有自己的缓冲区，相当耗费内存 （内存开销大，计算开销小，支持换装） 
        //2,使用动态方式，去实时去画 （内存开销小，计算开销大，支持换装,不建议使用）
        // var obj = this.skObj[name].buildArmature(2);
        var obj = this.skObj[name].buildArmature(iscache ? 0 : 1);
        // var obj = this.skObj[name].buildArmature();
        /*
        //事件
        obj.on(Laya.Event.LABEL, this, function (e) {

        });
        */
        //播放完毕
        obj.on(Laya.Event.STOPPED, this, function () {
            if (obj.playovercall) {
                var callfun = obj.playovercall;
                obj.playovercall = null;
                callfun(obj);
            }
            if (call) {
                obj.visible = call(obj);
            } else {
                obj.visible = false;
            }
        });
        obj.playovercall = null;
        obj.visible = false;
        return obj;
    },
    playSk: function (mCurrIndex, skobj, loop, playovercall, v1, v2) {
        if (skobj && skobj.templet) {
            skobj.playovercall = playovercall;
            skobj.visible = true;
            // skobj.play(mCurrIndex, loop, true);
            if (v1 != undefined) {
                skobj.play(mCurrIndex, loop, true, v1, v2);
                // skobj.play(mCurrIndex, loop, true, 1, 1);
            } else {
                skobj.play(mCurrIndex, loop, true);
            }

        }
    },
    cfg: {
        moshi: {//模式位置
            1: {
                x: 0, y: 0, name: "快速匹配"
            },
            2: {
                x: 125 + 22, y: 0, name: "猎鸟"
            },
            3: {
                x: 0, y: 125 + 22, name: "水果杂技"
            },
            4: {
                x: 125 + 22, y: 125 + 22, name: "好友对战"
            },
            5: {
                x: 0, y: (125 + 22) * 2, name: "匹配奖励"
            }
        }
    }
}